package de.thm.exa.holders;

import java.util.HashMap;

import de.thm.exa.helpers.Debugger;

public class ChainedPageMap extends HashMap<Long, ChainedPageList>
{
    private long INSERTS = 0;
    private long COLLISIONS = 0;
    
    public ChainedPageList put(Keyword keyword, Long key, Page value)
    {
        if (Debugger.COUNT_INSERTS) { INSERTS++; }
        
        ChainedPageList list;

        if( this.containsKey(key) )
        {
        	list = this.get(key);
        	if ( list.getKeyword().equals(keyword) )
        	{
        		list.add( value );
        	}
        	else
        	{
	    		while ( list.hasNextPageList() )
	    		{
		    		list = list.nextPageList();

		        	if ( list.getKeyword().equals(keyword) )
		        	{
		                list.add( value );
			        	return list;
		        	}
	    		}
                if (Debugger.COUNT_COLLISIONS) { COLLISIONS++; }
                ChainedPageList newlist = new ChainedPageList( keyword );
                newlist.add( value );
                list.setNextPageList( newlist );
        	}
        }
        else
        {
            list = new ChainedPageList( keyword );
            list.add(value);
            this.put(key, list);
        }
        
        return list;
    }

    @Override
    public ChainedPageList get(Object key)
    {
        return super.get(key);
    }
    
    public long getInserts() { return INSERTS; }
    public long getCollisions() { return COLLISIONS; }
}
